package org.example.proxy.jdk;

import java.io.*;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, FileNotFoundException {
//        HelloService helloService = new StaticProxy();
//        String staticProxy = helloService.sayHello("static proxy");
//        System.out.println(staticProxy);
//
        DynamicProxyInvocationHandler dynamicProxyInvocationHandler = new DynamicProxyInvocationHandler(new HelloServiceImpl());
        Class<?> proxyClass = Proxy.getProxyClass(HelloService.class.getClassLoader(), HelloService.class);
        HelloService helloService1 = (HelloService) proxyClass.getConstructor(InvocationHandler.class)
                .newInstance(dynamicProxyInvocationHandler);
        String otherDynamicProxy = helloService1.sayHello("other dynamic proxy");
        System.out.println(otherDynamicProxy);

        HelloService service = (HelloService) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{HelloService.class, RegionService.class}, new DynamicProxyInvocationHandler(new HelloServiceImpl()));
        String dynamicProxy = service.sayHello("Dynamic Proxy");
        System.out.println(dynamicProxy);


//        byte[] generateProxyClass = ProxyGenerator.generateProxyClass("customer", new Class[]{HelloService.class,RegionService.class},Modifier.PRIVATE);
//        try (FileOutputStream fileOutputStream = new FileOutputStream("customer.class")) {
//            fileOutputStream.write(generateProxyClass);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}
